/**
 * 
 */
package com.et114.modules.products.proservices;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.et114.core.action.BaseDispatchAction;
import com.et114.core.common.AppContext;
import com.et114.core.common.CONST;
import com.et114.core.utility.NumberUtils;
import com.et114.modules.products.commons.Const;
import com.et114.modules.products.commons.PorJsonObj;
import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.vo.BizOption;

/*
 * @author guanhw
 */
public class ProServiceAction extends BaseDispatchAction {
	@ SuppressWarnings ( "unchecked" )
	public  ActionForward proServiceIndex( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC
		List< BizOption >  bssOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_SERVICE_PLAN_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "bssOps" , bssOps ) ;
		
		//PARAMS
		ProServiceForm proServiceForm = ( ProServiceForm ) form ;
		Byte proType = proServiceForm.getProType ( ) ; 
		Long proId = proServiceForm.getProId ( ) ; 
		if ( NumberUtils.isEmptyOrZero( proType ) || NumberUtils.isEmptyOrZero( proId ) ) 
			throw new Exception( "proType or proId is NULL !" ) ;
		
		
		//BIS
		ProService vo = new ProService( ) ; 
		PropertyUtils.copyProperties ( vo , proServiceForm ) ;
		request.setAttribute ( "vo" , vo ) ;
		return mapping.findForward ( "proServiceIndex" ) ;
	}
	
	@ SuppressWarnings ( "unchecked" )
	public ActionForward getProService( ActionMapping mapping , ActionForm form , 
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC
		List< BizOption >  bssOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_SERVICE_PLAN_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "bssOps" , bssOps ) ;
	
		//PARAMS
		ProServiceForm proServiceForm = ( ProServiceForm ) form ;
		Byte proType = proServiceForm.getProType ( ) ; 
		Long proId = proServiceForm.getProId ( ) ; 
		
		Long proServiceId = proServiceForm.getProServiceId ( ) ; 
		
		ProService vo = new ProService( ) ; 
		PropertyUtils.copyProperties ( vo , proServiceForm ) ;
		//BIS
		ProServiceServiceImpl pssi = ( ProServiceServiceImpl ) getBean( "proServiceServiceImpl" ) ; 
		
		if ( NumberUtils.isEmptyOrZero ( proServiceId ) ) { //loads
			if ( NumberUtils.isEmptyOrZero( proType ) || NumberUtils.isEmptyOrZero( proId ) ) 
				throw new Exception( "proType or proId is NULL !" ) ;
			
			List< ProService > proServices = pssi.getProServices ( vo ) ;
			request.setAttribute ( "proServices" , proServices ) ;
			request.setAttribute ( "vo" , vo ) ; 
			return mapping.findForward ( "proServices" ) ; 
		}
		else { //load one
			vo = pssi.getProServiceById ( proServiceId ) ; 
			request.setAttribute ( "proService" , vo ) ; 
			return mapping.findForward ( "editProService" ) ; 
		}
		
	}
	
	public ActionForward toEditProService( ActionMapping mapping , ActionForm form , 
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		ProServiceForm proServiceForm = ( ProServiceForm ) form ;
		Byte proType = proServiceForm.getProType ( ) ; 
		Long proId = proServiceForm.getProId ( ) ; 
		
		ProService vo = new ProService( ) ; 
		PropertyUtils.copyProperties ( vo , proServiceForm ) ;

		if ( NumberUtils.isEmptyOrZero( proType ) || NumberUtils.isEmptyOrZero( proId ) ) 
			throw new Exception( "proType or proId is NULL !" ) ;
		
		request.setAttribute ( "vo" , vo ) ;
		return mapping.findForward ( "editProService" ) ; 
	}
	
	
	public ActionForward editProService( ActionMapping mapping , ActionForm form , 
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC 
		
		//PARAMS
		ProServiceForm proServiceForm = ( ProServiceForm ) form ;
		Long proServiceId = proServiceForm.getProServiceId ( ) ; 

		Byte proType = proServiceForm.getProType ( ) ; 
		Long proId = proServiceForm.getProId ( ) ; 
		
		ProService vo = new ProService( ) ; 
		PropertyUtils.copyProperties ( vo , proServiceForm ) ;
		
		
		if ( NumberUtils.isEmptyOrZero( proType ) || NumberUtils.isEmptyOrZero( proId ) ) 
			throw new Exception( "proType or proId is NULL !" ) ;
		
		//BIS
		ProServiceServiceImpl pssi = ( ProServiceServiceImpl ) getBean( "proServiceServiceImpl" ) ; 
		if ( NumberUtils.isEmptyOrZero ( proServiceId ) ) { //new
			proServiceId = pssi.saveProService ( vo ) ; 
		}
		else  //update 
			pssi.updateProService ( vo ) ; 
		
		
		return new ActionForward( "proservice.go?method=getProService&proServiceId=" + proServiceId , true ) ; 
	}
	
	public ActionForward removeProService( ActionMapping mapping , ActionForm form , 
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		ProServiceForm proServiceForm = ( ProServiceForm ) form ;
		//params
		Long proServiceTypeId = proServiceForm.getProServiceTypeId ( ) ; 
		Long proId = proServiceForm.getProId ( ) ; 
		Byte proType = proServiceForm.getProType ( ) ; 
		
		if ( NumberUtils.isEmptyOrZero( proType ) || NumberUtils.isEmptyOrZero( proId ) ) 
			throw new Exception( "proType or proId is NULL !" ) ;
		
		//BIS
		ProServiceServiceImpl pssi = ( ProServiceServiceImpl ) getBean( "proServiceServiceImpl" ) ; 
		Long[] deleteIds = proServiceForm.getDeleteId ( ) ; 
		if ( deleteIds != null ) {
			for ( Long deleteId : deleteIds ) {
				//ProService vo = pssi.getProServiceById ( deleteId ) ; 
				//vo.setRemoveMark ( String.valueOf ( Const.COMMON_REMOVE_MARK_Y ) ) ; 
				//pssi.updateProService ( vo ) ;
				pssi.removceProService ( deleteId ) ;
			}
		}
		
		return new ActionForward (
				"proservice.go?method=getProService&proServiceTypeId="
						+ proServiceTypeId + "&proId=" + proId + "&proType=" + proType , true );
	}
	
	
	///////////////////////////////////////////////
	
	@ SuppressWarnings ( "unchecked" )
	public ActionForward getProService4H( ActionMapping mapping , ActionForm form , 
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		response.setContentType ( "text/html;charset="
				+ AppContext.getFrameWorkPropertiesBean ( ).getCharSet ( ) );
		long ct = System.currentTimeMillis ( ) ; 
		//DIC
		List< BizOption >  bssOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_SERVICE_PLAN_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "bssOps" , bssOps ) ;
	
		//PARAMS
		ProServiceForm proServiceForm = ( ProServiceForm ) form ;
		Byte proType = proServiceForm.getProType ( ) ; 
		Long proId = proServiceForm.getProId ( ) ; 
		
		
		ProService vo = new ProService( ) ; 
		PropertyUtils.copyProperties ( vo , proServiceForm ) ;
		//BIS
		ProServiceServiceImpl pssi = ( ProServiceServiceImpl ) getBean( "proServiceServiceImpl" ) ; 
		

		if ( NumberUtils.isEmptyOrZero( proType ) || NumberUtils.isEmptyOrZero( proId ) || NumberUtils.isEmptyOrZero( vo.getProServiceTypeId ( ) ) ) 
			return PorJsonObj.response ( response , PorJsonObj.error (
					CONST.COUNTRY_ZH ).toString ( ) );
		
		List< ProService > proServices = pssi.getProServices ( vo ) ;
		ProServiceType pst = null ; 
		for ( ProService proSerivce : proServices ) {
			proSerivce.setProServiceType( null ) ; 
			for ( BizOption  bo : bssOps ) {
				if ( proSerivce.getProServiceTypeId ( ).equals ( Long.valueOf ( bo.getOptionValue ( ) ) ) ) {
					pst = new ProServiceType( Long.valueOf ( bo.getOptionValue ( ) ) , bo.getOptionName ( ) , null ) ;
					proSerivce.setProServiceType( pst ) ; 
				}
			}
		}
	
		logger.info ( "generate proService time:" + ( System.currentTimeMillis ( ) - ct ) ) ;
		return  PorJsonObj.response ( response , PorJsonObj.getProSerivices ( proServices ) ) ;
	}
	

	@ SuppressWarnings ( "unchecked" )
	public ActionForward getProServiceInfo4View( ActionMapping mapping , ActionForm form , 
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		response.setContentType ( "text/html;charset="
				+ AppContext.getFrameWorkPropertiesBean ( ).getCharSet ( ) );

		long ct = System.currentTimeMillis ( ) ; 
		//DIC
		List< BizOption >  bssOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_SERVICE_PLAN_KEY ,  false  , getSessionLanguage ( request )  ) ;
		request.setAttribute ( "bssOps" , bssOps ) ;
	
		//PARAMS
		ProServiceForm proServiceForm = ( ProServiceForm ) form ;
		Byte proType = proServiceForm.getProType ( ) ; 
		Long proId = proServiceForm.getProId ( ) ; 
		Long proServiceTypeId = proServiceForm.getProServiceTypeId ( ) ; 
		
		
		ProService vo = new ProService( ) ; 
		PropertyUtils.copyProperties ( vo , proServiceForm ) ;
		//BIS
		ProServiceServiceImpl pssi = ( ProServiceServiceImpl ) getBean( "proServiceServiceImpl" ) ; 
		
		

		List< ProService > pts  = pssi.getProserviceTypeByPro( vo ) ; 
		if ( NumberUtils.isEmptyOrZero( proServiceTypeId ) && pts.size ( ) > 0  ) {
			proServiceTypeId = ( ( ProService )pts.get ( 0 ) ) .getProServiceTypeId ( ) ; 
			vo.setProServiceTypeId ( proServiceTypeId ) ;
		}		
		
		List< ProServiceType > proServiceTypes = new ArrayList< ProServiceType >( ) ;  
		for ( ProService proSerivce : pts ) {
			ProServiceType  pst = null ; 
			proSerivce.setProServiceType( null ) ; 
			for ( BizOption  bo : bssOps ) {
				if ( proSerivce.getProServiceTypeId ( ).equals ( Long.valueOf ( bo.getOptionValue ( ) ) ) ) {
					pst = new ProServiceType( ) ; 
					pst.setProServiceTypeId ( proSerivce.getProServiceTypeId ( ) ) ; 
					pst.setProServiceName ( bo.getOptionName ( ) ) ; 
					proServiceTypes.add ( pst ) ; 
				}
			}
		}

		String json = PorJsonObj.getProSeriviceTypes ( proServiceTypes ).toString ( ) ; 
		

		if ( NumberUtils.isEmptyOrZero( proType ) || NumberUtils.isEmptyOrZero( proId ) || NumberUtils.isEmptyOrZero( proServiceTypeId ) ) 
			return PorJsonObj.response ( response , PorJsonObj.error (
					getSessionLanguage ( request ) ).toString ( ) );
		
		List< ProService > proServices = pssi.getProservice4View ( vo ) ;
		ProServiceType pst = null ; 
		for ( ProService proSerivce : proServices ) {
			proSerivce.setProServiceType( null ) ; 
			for ( BizOption  bo : bssOps ) {
				if ( proSerivce.getProServiceTypeId ( ).equals ( Long.valueOf ( bo.getOptionValue ( ) ) ) ) {
					pst = new ProServiceType( Long.valueOf ( bo.getOptionValue ( ) ) , bo.getOptionName ( ) , null ) ;
					proSerivce.setProServiceType( pst ) ; 
				}
			}
		}
	
		logger.info ( "generate proService time:" + ( System.currentTimeMillis ( ) - ct ) ) ;
		return  PorJsonObj.response ( response , PorJsonObj.getProSerivices ( proServices ).element ( "proServiceType" , json ) ) ;
	}
	
	
	@ SuppressWarnings ( "unchecked" )
	public ActionForward getProService4View( ActionMapping mapping , ActionForm form , 
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		response.setContentType ( "text/html;charset="
				+ AppContext.getFrameWorkPropertiesBean ( ).getCharSet ( ) );
		long ct = System.currentTimeMillis ( ) ; 
		//DIC
		List< BizOption >  bssOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_SERVICE_PLAN_KEY ,  false  , getSessionLanguage ( request )  ) ;
		request.setAttribute ( "bssOps" , bssOps ) ;
	
		//PARAMS
		ProServiceForm proServiceForm = ( ProServiceForm ) form ;
		Byte proType = proServiceForm.getProType ( ) ; 
		Long proId = proServiceForm.getProId ( ) ; 
		
		
		ProService vo = new ProService( ) ; 
		PropertyUtils.copyProperties ( vo , proServiceForm ) ;
		//BIS
		ProServiceServiceImpl pssi = ( ProServiceServiceImpl ) getBean( "proServiceServiceImpl" ) ; 
		
		
		if ( NumberUtils.isEmptyOrZero( proType ) || NumberUtils.isEmptyOrZero( proId )  ) 
			return PorJsonObj.response ( response , PorJsonObj.error (
					getSessionLanguage ( request ) ).toString ( ) );

		
		

			List< ProService > pts  = pssi.getProserviceTypeByPro( vo ) ; 
			ProServiceType pst = null ; 
			for ( ProService proSerivce : pts ) {
				proSerivce.setProServiceType( null ) ; 
				for ( BizOption  bo : bssOps ) {
					if ( proSerivce.getProServiceTypeId ( ).equals ( Long.valueOf ( bo.getOptionValue ( ) ) ) ) {
						pst = new ProServiceType( Long.valueOf ( bo.getOptionValue ( ) ) , bo.getOptionName ( ) , null ) ;
						proSerivce.setProServiceType( pst ) ; 
					}
				}
			}			

			List< ProService > proServices = pssi.getProservice4View ( vo ) ;
			logger.info ( "generate proService time:" + ( System.currentTimeMillis ( ) - ct ) ) ;
			return  PorJsonObj.response ( response , PorJsonObj.getProSerivicesByTypeGroup( pts , proServices ) .toString ( )  ) ;
	}	
	
}
